/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSMesh.cpp
 *
 *  Created on: 2010/02/21
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSMesh.h"
#include "RSPrimitivesElement.h"
#include "RSVertices.h"

namespace Realsphere {

    RSMesh::RSMesh () :
        RSGeometricElement ( RS_MESH )
    {
        mVertices = NULL;
    }

    RSMesh::~RSMesh ()
    {
        mPrimitivesElements.clear ();
        mVertices = NULL;
    }

    RSPrimitivesElement* RSMesh::GetPrimitivesElement ( unsigned int rIndex ) const
    {
        return mPrimitivesElements[rIndex];
    }

    int RSMesh::IndexOf ( RSPrimitivesElement* rPrimitivesElement )
    {
        return mPrimitivesElements.indexOf ( rPrimitivesElement );
    }

    unsigned int RSMesh::PrimitivesElementsSize ()
    {
        return mPrimitivesElements.size ();
    }

    RSPrimitivesElements RSMesh::PrimitivesElements () const
    {
        return mPrimitivesElements;
    }

    RSPrimitivesElementsIterator RSMesh::PrimitivesElementsIterator () const
    {
        return RSPrimitivesElementsIterator ( mPrimitivesElements );
    }

    RSVertices* RSMesh::Vertices () const
    {
        return mVertices;
    }

    RSError RSMesh::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSGeometricElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {
            if ( rChild->InstanceOf ( RS_PRIMITIVES_ELEMENT_CLASS )
                    || rChild->InstanceOf ( RS_VERTICES_CLASS ) )
            {
                error = RS_NO_ERROR;
            }
        }

        return error;
    }

    RSError RSMesh::BeforeRemoveChildElement ( RSElement* rChild )
    {
        RSError error = RSGeometricElement::BeforeRemoveChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {
            if ( rChild->InstanceOf ( RS_PRIMITIVES_ELEMENT_CLASS )
                    || rChild->InstanceOf ( RS_VERTICES_CLASS ) )
            {
                error = RS_NO_ERROR;
            }
        }

        return error;
    }

    bool RSMesh::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSGeometricElement::AfterAddChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_PRIMITIVES_ELEMENT_CLASS ) )
        {
            RSPrimitivesElement* primitive = RSPrimitivesElementCast ( rChild );
            mPrimitivesElements.append ( primitive );
            handled = true;

            emit PrimitiveElementAdded ( primitive );
        }
        else if ( !handled && rChild->InstanceOf ( RS_VERTICES_CLASS ) )
        {
            if ( mVertices != NULL )
                RemoveChildElement ( mVertices );

            mVertices = RSVerticesCast ( rChild );
            handled = true;

            emit VerticesAdded ( mVertices );
        }

        return handled;
    }

    bool RSMesh::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSGeometricElement::AfterRemoveChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_PRIMITIVES_ELEMENT_CLASS ) )
        {

            RSPrimitivesElement* primitive = RSPrimitivesElementCast ( rChild );
            int primitive_index = mPrimitivesElements.indexOf ( primitive );
            mPrimitivesElements.removeAt ( primitive_index );

            handled = true;

            emit PrimitiveElementAdded ( primitive );
        }
        else if ( !handled && rChild->InstanceOf ( RS_VERTICES_CLASS ) )
        {
            RSVertices* vertices = RSVerticesCast ( rChild );
            mVertices = NULL;
            handled = true;

            emit VerticesRemoved( vertices );
        }

        return handled;
    }

    RSError RSMesh::AddPrimitiveElement ( RSPrimitivesElement* rPrimitiveElement )
    {
        RSError error = AddChildElement ( rPrimitiveElement );
        return error;
    }

    RSError RSMesh::RemovePrimitiveElement ( RSPrimitivesElement* rPrimitiveElement )
    {
        RSError error = RemoveChildElement ( rPrimitiveElement );
        return error;
    }

    RSError RSMesh::RemovePrimitiveElement ( int rIndex )
    {
        RSPrimitivesElement* primitive = mPrimitivesElements[rIndex];
        RSError error = RemovePrimitiveElement ( primitive );
        return error;
    }

    RSError RSMesh::SetVertices ( RSVertices* rVertices )
    {
        RSError error = AddChildElement ( rVertices );
        return error;
    }
}
